<?php

class sys_login extends a_admin_controller {

    protected $form_data;
    protected $form_errors;
    protected $srv;

    public function __construct($post_handling_enabled = FALSE) {
        parent::__construct($post_handling_enabled);
        $this->srv = new user_service_l_user();
        if (is_sys_user_logged_in()) {
            $this->do_redirect("<h2> You are already logged in as SYSTEM user!</h2>", 2);
        }
        $this->__build_form();
    }

    public function do_partial_rendering($do_partially=NULL) {
        return TRUE;
    }


    public function batch_assign() {
        $this->assign("form_data", $this->form_data);
        $this->assign("form_error", $this->form_errors);
    }

    public function index() {

    }

    protected function __build_form() {
        $this->form
                ->open('user/admin/sys_login/process')
                ->text('username', 'Username', 'required|xss_clean|trim')
                ->pass('password', 'Password', 'required|xss_clean')
                ->indent(200)
                ->submit()
                ->reset();
        $this->form_data = $this->form->get();
        $this->form_errors = $this->form->errors;
    }

    public function process() {
        if ($this->form->valid) {            
            if ($this->srv->login($this->input->post('username'), $this->input->post('password'),TRUE) !== NULL) {
//                event_dispatcher::i()->fire(module_u_user::EVENT_LOGIN_SUCCESS);
//                $this->do_redirect("<h2 style='font-weight:normal'>You have successfully logged into the system as SYSTEM user</h2>..redirecting in 3 seconds..",3,"core/admin/home");
               redirect("http://ecopoxysystemscanada.com/ecopoxysystemscanada/index.php/analytics/admin/stats");
            } else {
                $this->form_errors = "Could not login as SYSTEM user!";
            }
        } else {
            $this->form_errors = $this->form->errors;
        }
    }

}

?>
